//
//  AddYunDanEditGoodVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/17.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "AddYunDanEditGoodVC.h"
#import "AddYunDanEditGoodCell.h"

@interface AddYunDanEditGoodVC () <UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation AddYunDanEditGoodVC

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"保存" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:nextBtn];
    }
    return _bottomView;
}

- (void)saveClick {
    __block BOOL canGo = YES;
    [self.billModel.list enumerateObjectsUsingBlock:^(AddYunDanTiHuoGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.selected) {
            if (!obj.default_num || !obj.default_jnum) {
                canGo = NO;
                [MBProgressHUD leShowFailedHUD:self.view message:@"所选货物数量和件数不能为0"];
                return;
            }
        }
    }];
    if (canGo) {
        if (self.finishBlock) {
            self.finishBlock();
        }
        [self.rt_navigationController popViewControllerAnimated:YES];
    }
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(15, 0, ScreenW - 30, ScreenH - self.bottomView.nim_height - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑提货清单";
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.billModel.list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddYunDanEditGoodCell *cell = [AddYunDanEditGoodCell AddYunDanEditGoodCellWithTableView:tableView];
    AddYunDanTiHuoGoodModel *model = self.billModel.list[indexPath.row];
    cell.model = model;
    cell.selectBlock = ^{
        model.selected = !model.selected;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    cell.openBlock = ^{
        model.isOpen = !model.isOpen;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    __weak_self;
    cell.countBlock = ^(NSString * _Nonnull str) {
        if ([str intValue] > model.num) {
            [MBProgressHUD leShowFailedHUD:weakself.view message:[NSString stringWithFormat:@"数量不能大于%d",model.num]];
            model.default_num = 0;
            model.default_jnum = 0;
        }else {
            model.default_num = [str intValue];
            model.default_jnum = model.default_num % model.takeNum > 0 ? (model.default_num / model.takeNum) + 1 : model.default_num / model.takeNum;
        }
        
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    cell.pieceBlock = ^(NSString * _Nonnull str) {
        if ([str intValue] > model.pnum) {
            [MBProgressHUD leShowFailedHUD:weakself.view message:[NSString stringWithFormat:@"件数不能大于%d",model.pnum]];
            model.default_num = 0;
            model.default_jnum = 0;
        }else {
            model.default_jnum = [str intValue];
            model.default_num = model.default_jnum * model.takeNum;
        }
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddYunDanEditGoodCell *cell = [AddYunDanEditGoodCell AddYunDanEditGoodCellWithTableView:tableView];
    AddYunDanTiHuoGoodModel *model = self.billModel.list[indexPath.row];
    cell.model = model;
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.nim_width, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.nim_width, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
